package com.xg.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xg.sys.entity.SysUser;
import com.xg.sys.entity.SysUserRole;
import com.xg.sys.mapper.SysUserRoleMapper;
import com.xg.sys.service.SysUserRoleService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户与角色对应关系 服务实现类
 * </p>
 *
 * @author ChenLiang
 * @since 2019-04-17
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {
	@Resource
    SysUserRoleMapper sysUserRoleMapper;

    @Override
    public void saveOrUpdate(Long id, List<Long> roleIdList) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        baseMapper.deleteByMap(map);
        if (roleIdList == null || roleIdList.size() == 0) {
            return;
        }//保存用户与角色关系
        List<SysUserRole> list = new ArrayList<>(roleIdList.size());
        for(Long roleId : roleIdList){
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(id);
            sysUserRole.setRoleId(roleId);
            list.add(sysUserRole);
        }
        this.saveBatch(list);
    }

    @Override
    public List<Long> queryRoleIdList(Long id) {
        return baseMapper.queryRoleIdList(id);
    }

    @Override
    public int deleteBatch(Long[] ids) {
        return baseMapper.deleteBatch(ids);
    }

//	@Override
//	public void getPageList(Page<SysRole> page, SysRole role) {
//
//		QueryWrapper<SysRole> queryWrapper = new QueryWrapper<SysRole>();
//
//
//		if (!StringUtils.isEmpty(role.getRoleName())) {
//			queryWrapper.like("role_name", role.getRoleName());
//		}
//		queryWrapper.orderByAsc("role_name");
//		//执行
//		baseMapper.selectPage(page,queryWrapper);
//
//	}

	@Override
	public List<Long> getList(Long id) throws Exception{
        List<Long> roleIdList = sysUserRoleMapper.queryRoleIdList(id);
        /*StringBuffer sql=new StringBuffer("SELECT e.id FROM sys_role e WHERE EXISTS (SELECT id FROM sys_user_role o WHERE e.id=o.role_id ");
//		if(!StringUtils.isEmpty(id)){
			sql.append(" AND o.user_id='"+id+"')");
//		}
		List<Map<String, Object>> list=sqlAdapterMapper.selectSQL(sql.toString());
		List<String> idList = new ArrayList<>();
		for (Map<String, Object> map : list) {
			idList.add(String.valueOf(map.get("id")));
		}*/
		return roleIdList;
	}

    @Override
    public Boolean needStaCode() {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        Long userId = sysUser.getId();
        List<SysUserRole> list = this.list(new QueryWrapper<SysUserRole>().lambda()
                .select(SysUserRole::getRoleId)
                .eq(SysUserRole::getUserId, userId)
        );

        return true;
    }

    @Override
    public void relateRole(List<Long> roleIds, Long userId) {
        removeByUserId(userId);
        List<SysUserRole> userRoleList = roleIds.stream()
                .map(item -> new SysUserRole().setRoleId(item).setUserId(userId))
                .collect(Collectors.toList());
        this.saveBatch(userRoleList);
    }

    @Override
    public boolean removeByUserId(Long userId) {
        return this.remove(
                new LambdaQueryWrapper<SysUserRole>()
                        .eq(SysUserRole::getUserId, userId)
        );
    }

    @Override
    public List<SysUserRole> getRidListByUidList(List<Long> uidList) {
        return this.list(
                new LambdaQueryWrapper<SysUserRole>()
                        .in(SysUserRole::getUserId, uidList)
        );
    }

    @Override
    public List<SysUserRole> getUserRoleByRidList(List<Long> ridList) {
        return this.list(
                new LambdaQueryWrapper<SysUserRole>()
                        .in(SysUserRole::getRoleId, ridList)
        );
    }
}
